package cn.zjj.order.service.impl;

import cn.zjj.basic.service.impl.BaseServiceImpl;
import cn.zjj.basic.util.PageList;
import cn.zjj.order.domain.OrderPetAcquisition;
import cn.zjj.order.dto.OrderDto;
import cn.zjj.order.mapper.OrderPetAcquisitionMapper;
import cn.zjj.order.query.OrderPetAcquisitionQuery;
import cn.zjj.order.service.IOrderPetAcquisitionService;
import cn.zjj.org.domain.Employee;
import cn.zjj.org.mapper.EmployeeMapper;
import cn.zjj.org.mapper.ShopMapper;
import cn.zjj.pet.domain.Pet;
import cn.zjj.pet.domain.SearchMasterMsg;
import cn.zjj.pet.mapper.PetMapper;
import cn.zjj.pet.mapper.SearchMasterMsgAuditLogMapper;
import cn.zjj.pet.mapper.SearchMasterMsgMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopMapper shopMapper;


    @Autowired
    private PetMapper petMapper;

    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;

    @Override
    public PageList<OrderPetAcquisition> selectQuery(OrderPetAcquisitionQuery query, Long logininfoId) {
        Employee employee = employeeMapper.selectByLogininfoId(logininfoId);
        Long shop_id = employee.getShop_id();
        if(shop_id!=null){//店铺的
            //店铺进入筛选池
            query.setShop_id(shop_id);
            if(shopMapper.selectById(shop_id).getAdmin_id().equals(employee.getId())){
                //单个员工处理的订单进入筛选池
                query.setEmp_id(employee.getId());
            }
        }
        return super.selectQuery(query);
    }


    //取消订单
    @Override
    public void cancel(Long id) {

        OrderPetAcquisition order = orderPetAcquisitionMapper.selectById(id);

        //删除宠物和详情   删除宠物内自带删除详情
        petMapper.delete(order.getPet_id());

        //清空寻主信息店铺id
        searchMasterMsgMapper.reject(order.getSearch_master_msg_id());

        //改订单状态
        orderPetAcquisitionMapper.updateState(order.getId()); //没其他数据不能直接将状态更新为-1,需要单独更改状态
    }


    //订单确认
    @Override
    public void confirm(OrderDto dto) {
        BigDecimal money = dto.getMoney();
        Long id = dto.getId();
        Long payType = dto.getPayType();
        OrderPetAcquisition order = orderPetAcquisitionMapper.selectById(id);
        order.setState(1);
        order.setPrice(money);
        order.setPaytype(payType);
        orderPetAcquisitionMapper.update(order);  //有其他数据,更新其他数据不会为空

        //更改寻主状态
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.selectById(order.getSearch_master_msg_id());
        searchMasterMsg.setState(2);
        searchMasterMsg.setNote("收购完成!");
        searchMasterMsgMapper.update(searchMasterMsg);
    }
}
